package com.gw.config;

import java.util.HashMap;
import java.util.Map;

import org.beetl.core.GroupTemplate;
import org.beetl.ext.jfinal.BeetlRenderFactory;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.druid.wall.WallFilter;
import com.gw.common.model._MappingKit;
import com.gw.common.model.Information;
import com.gw.common.model._MappingKit;
import com.gw.controller.Admin;
import com.gw.controller.Index;
import com.gw.controller.Manager_admin;
import com.gw.controller.Manager_info;
import com.gw.controller.Manager_permission;
import com.gw.routes.AdminRoutes;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.SqlServerDialect;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;

public class Config extends JFinalConfig {

	@Override
	public void configConstant(Constants me){
		// TODO Auto-generated method stub
		loadProp("a_little_config_dev.txt", "a_little_config.txt");
		me.setDevMode(PropKit.getBoolean("devMode", false));
		me.setEncoding("utf-8");
		me.setMainRenderFactory(new BeetlRenderFactory());
		// 根据gt可以添加扩展函数，格式化函数，共享变量等，
		GroupTemplate gt = BeetlRenderFactory.groupTemplate;
		Map<String, Object> shared = new HashMap<String, Object>();
		shared.put("static_domain", PropKit.get("static_domain"));
		gt.setSharedVars(shared);
	}

	@Override
	public void configRoute(Routes me) {
		// TODO Auto-generated method stub
		me.add("/", Index.class, "/template/admin");
		me.add(new AdminRoutes());
//		me.add("/admin", Admin.class, "/template/admin");
//		me.add("/manager_info", Manager_info.class, "/template/admin");
//		me.add("/manager_admin", Manager_admin.class, "/template/admin");
//		me.add("/manager_permission", Manager_permission.class, "/template/admin");
//		me.add("/information", Manager_info.class, "/template/admin");
	}

	@Override
	public void configPlugin(Plugins me) {
		// TODO Auto-generated method stub
		DruidPlugin dpSQLServer = new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password"),
				"com.microsoft.sqlserver.jdbc.SQLServerDriver");
		dpSQLServer.addFilter(new StatFilter());
		WallFilter wallSQLServer = new WallFilter();
		wallSQLServer.setDbType(JdbcConstants.SQL_SERVER);
		dpSQLServer.addFilter(wallSQLServer);
		me.add(dpSQLServer);

		// 配置ActiveRecord插件
		ActiveRecordPlugin arpSQLServer = new ActiveRecordPlugin("sqlserver", dpSQLServer);
		arpSQLServer.setDialect(new SqlServerDialect());
		arpSQLServer.setShowSql(true);
		me.add(arpSQLServer);
		
		DruidPlugin dbmysql = new DruidPlugin(PropKit.get("jdbcUrl_mysql"), PropKit.get("user_mysql"),PropKit.get("password_mysql").trim());
		dbmysql.addFilter(new StatFilter());
		WallFilter wallMysql = new WallFilter();
		dbmysql.addFilter(wallMysql);
		me.add(dbmysql);
		ActiveRecordPlugin arpmysql = new ActiveRecordPlugin("mysql", dbmysql);
	    arpmysql.setShowSql(true);
        me.add(arpmysql);
        _MappingKit.mapping(arpmysql);
		
//		DruidPlugin dp = new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password"),
//				"com.microsoft.sqlserver.jdbc.SQLServerDriver");
//		dp.addFilter(new StatFilter());
//		WallFilter wall = new WallFilter();
//		wall.setDbType(JdbcConstants.SQL_SERVER);
//		dp.addFilter(wall);
//		me.add(dp);
//
//		// 配置ActiveRecord插件
//		ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
//		arp.setDialect(new SqlServerDialect());
//		arp.setShowSql(true);
//		me.add(arp);
		
//		arp.addMapping("ezoffice.OA_INFORMATION", "INFORMATION_ID", Information.class);
	}

	@Override
	public void configInterceptor(Interceptors me) {
		// TODO Auto-generated method stub

	}

	@Override
	public void configHandler(Handlers me) {
		// TODO Auto-generated method stub
		// beetl自带 ${ctxPath}
		// me.add(new ContextPathHandler("path"));
	}
//	public static void main(String[] args){
//		JFinal.start("WebRoot", 8088, "/jtt_wap", 5);
//	}
	/**
	 * 如果生产环境配置文件存在，则优先加载该配置，否则加载开发环境配置文件
	 * 
	 * @param pro
	 *            生产环境配置文件
	 * @param dev
	 *            开发环境配置文件
	 */
	public void loadProp(String dev, String pro) {
		try {
			PropKit.use(dev);
		} catch (Exception e) {
			PropKit.use(pro);
		}
	}

}
